package com.itheima.user.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/info")
public class UserController {
    //添加cookie
    @RequestMapping("/addInfo")
    public String add(@RequestParam("key") String key,
                      @RequestParam("value") String value,
                      HttpServletResponse response) {
        Cookie cookie = new Cookie(key, value);
        cookie.setDomain("localhost");
        cookie.setPath("/");

        response.addCookie(cookie);

        return "redirect:/info/show.do";
    }

    //cookie验证成功展示页面
    @RequestMapping("/show")
    public String show(Model model, @CookieValue("heima") String cookieValue) {

        model.addAttribute("msg", cookieValue);
        return "showInfo";
    }
}
